package com.tangdada.whacmole;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.support.libs.common.Constant;

/**
 * Created by wjy on 2018/4/11.
 */

public class GameView extends SurfaceView implements Runnable, SurfaceHolder.Callback, View.OnTouchListener {
    Context mContext;
    SurfaceHolder mHolder;//主holder
    Thread mThread;//循环绘画
    Bitmap mBuffer = null;//二级缓存
    boolean mRun = false;
    private final static int FPS = 50;//刷新频率
    private boolean mOpenDoubleCache;//是否开启双缓冲
    private GameController mController;
    public static int sGameWidth, sGameHeight;
    public static float sScale_X;
    public static float sScale_Y;


    public GameView(Activity context, int width, int height) {
        super(context);
        this.sGameWidth = width;
        this.sGameHeight = height;
        sScale_X = sGameWidth / 750.0f;
        sScale_Y = sGameHeight / 1334.0f;

        init();
        setOnTouchListener(this);
        this.mContext = context;
    }

    public void setGameController(GameController controller){
        mController = controller;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (sGameWidth != 0 && sGameHeight != 0) {
            setMeasuredDimension(sGameWidth, sGameHeight);
        } else {
            sGameWidth = getMeasuredWidth();
            sGameHeight = getMeasuredHeight();
        }

    }

    private void init() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        mThread = new Thread(this);
    }

    public void setOpenDoubleCache(boolean open) {
        this.mOpenDoubleCache = open;
        if (mBuffer == null) {
            mBuffer = Bitmap.createBitmap(Constant.SCREEN_WIDTH
                    , Constant.SCREEN_HEIGHT, Bitmap.Config.ARGB_8888);//很重要创建二级缓存的必要方法
        }
    }

    @Override
    public void run() {
        try {
            Canvas c = null;
            while (mRun) {

                long startTime = System.currentTimeMillis();
                //获取画笔
                c = mHolder.lockCanvas();
                if (mOpenDoubleCache) {
                    Canvas c1 = new Canvas(mBuffer);
                    //清屏
                    c1.drawColor(Color.BLACK);
                    mController.onDraw(c1);
                    c.drawBitmap(mBuffer, 0, 0, new Paint());
                } else {
                    //清屏
                    c.drawColor(Color.BLACK);
                    mController.onDraw(c);
                }
                mHolder.unlockCanvasAndPost(c);
                long timeSpace = System.currentTimeMillis() - startTime;
                if (timeSpace < FPS) {
                    Thread.sleep(FPS - timeSpace);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mRun = true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        mThread = new Thread(this);
        mThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mRun = false;
        try {
            mThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mController.onTouchDown(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mController.onTouchMove(event);
                break;
            case MotionEvent.ACTION_UP:
                mController.onTouchUp(event);
                break;
        }
        return true;
    }

}